<?php

/**
 * This is the Widget for Update Resource.
 * 
 * @author Tuan Nguyen <nganhtuan63@gmail.com>
 * @version 1.0
 * @package cmswidgets.resource
 *
 */
class PaymentUpdateWidget extends CWidget {

    public $visible = true;

    public function init() {
        
    }

    public function run() {
        if ($this->visible) {
            $this->renderContent();
        }
    }

    protected function renderContent() {
        $model = new PaymentMethod;
        $is_new = false;
        $process = true;
        $types_array = ConstantDefine::fileTypes();

        $id = isset($_GET['id']) ? (int) $_GET['id'] : 0;
        $payment = PaymentMethod::model()->findByPk($id);
        if ($payment) {
            $model->payment_currency = $payment->payment_currency;
            $model->payment_name = $payment->payment_name;
            $model->payment_description = $payment->payment_description;
            $model->weight = $payment->weight;
            $model->status = $payment->status;
        }
        if (isset($_POST['ajax']) && $_POST['ajax'] === 'paymentmethod-form') {
            echo CActiveForm::validate($model);
            Yii::app()->end();
        }
        if (isset($_POST['PaymentMethod'])) {
            $model->attributes = $_POST['PaymentMethod'];
            $payment->payment_currency = $model->payment_currency;
            $payment->payment_name = $model->payment_name;
            $payment->payment_description = $model->payment_description;
            $payment->weight = $model->weight;
            $payment->status = $model->status;
            if ($payment->save()) {
                user()->setFlash('success', t('cms', 'Update Successfully!'));
            }
        }

        $this->render('cmswidgets.views.shopping.payment_create_widget', array('model' => $model, 'is_new' => $is_new, 'types_array' => $types_array));
    }

}
